package com.wskj.web.controller.system;

import com.wskj.common.annotation.Log;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.system.bo.SysNoticeReadAddBo;
import com.wskj.system.bo.SysNoticeReadEditBo;
import com.wskj.system.bo.SysNoticeReadQueryBo;
import com.wskj.system.service.ISysNoticeReadService;
import com.wskj.system.vo.web.SysNoticeReadVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 通知公告已读Controller
 *
 * @author wsdz
 * @date 2022-12-14
 */
@Api(value = "通知公告已读控制器", tags = {"通知公告已读管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/system/read")
public class SysNoticeReadController extends BaseController {

    private final ISysNoticeReadService iSysNoticeReadService;

    /**
     * 查询通知公告已读列表
     */
    @ApiOperation("查询通知公告已读列表")
    @PreAuthorize("@ss.hasPermi('system:read:list')")
    @GetMapping("/list")
    public TableDataInfo<SysNoticeReadVo> list(@Validated SysNoticeReadQueryBo bo) {
        return iSysNoticeReadService.queryPageList(bo);
    }

    /**
     * 导出通知公告已读列表
     */
    @ApiOperation("导出通知公告已读列表")
    @PreAuthorize("@ss.hasPermi('system:read:export')")
    @Log(title = "通知公告已读", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated SysNoticeReadQueryBo bo) {
        List<SysNoticeReadVo> list = iSysNoticeReadService.queryList(bo);
        ExcelUtil<SysNoticeReadVo> util = new ExcelUtil<>(SysNoticeReadVo.class);
        return util.exportExcel(list, "通知公告已读");
    }

    /**
     * 获取通知公告已读详细信息
     */
    @ApiOperation("获取通知公告已读详细信息")
    @PreAuthorize("@ss.hasPermi('system:read:query')")
    @GetMapping("/{readId}")
    public AjaxResult<SysNoticeReadVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("readId") Long readId) {
        return AjaxResult.success(iSysNoticeReadService.queryById(readId));
    }

    /**
     * 新增通知公告已读
     */
    @ApiOperation("新增通知公告已读")
    @PreAuthorize("@ss.hasPermi('system:read:add')")
    @Log(title = "通知公告已读", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysNoticeReadAddBo bo) {
        return toAjax(iSysNoticeReadService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改通知公告已读
     */
    @ApiOperation("修改通知公告已读")
    @PreAuthorize("@ss.hasPermi('system:read:edit')")
    @Log(title = "通知公告已读", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysNoticeReadEditBo bo) {
        return toAjax(iSysNoticeReadService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除通知公告已读
     */
    @ApiOperation("删除通知公告已读")
    @PreAuthorize("@ss.hasPermi('system:read:remove')")
    @Log(title = "通知公告已读" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{readIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] readIds) {
        return toAjax(iSysNoticeReadService.deleteWithValidByIds(Arrays.asList(readIds), true) ? 1 : 0);
    }

    /**
     * 导入通知公告已读数据
     */
    @ApiOperation("导入通知公告已读数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('system:read:import')")
    @Log(title = "通知公告已读" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<SysNoticeReadVo> util = new ExcelUtil<>(SysNoticeReadVo.class);
        List<SysNoticeReadVo> sysNoticeReadList;
        try {
            sysNoticeReadList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iSysNoticeReadService.importSysNoticeRead(sysNoticeReadList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<SysNoticeReadVo> util = new ExcelUtil<>(SysNoticeReadVo.class);
        return util.importTemplateExcel("通知公告已读数据");
    }
}
